/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare",
    "scprit/followup/app/datatable/datatable.servers.js",
    "scprit/followup/app/datatable/datatable.ui.js",],function($,$bb,$datatableServers,$datatableUi){
	var servers = {};
	var URL = $bb.baseUrl("followup");

    servers.ajaxQueue = function(callback){
        $bb.ajax({
            url: URL + "followup/service/queue/followQueueListQuery",
            success: function (data) {
                if(data.success){
                    callback(data.data.rows);
                }
            }
        });
    }
    /**
     * 入参
     */
    var inParams = function(args){
        var ageFrom, ageTo;
        if($("#ageFrom-year").val() !="" || $("#ageFrom-month").val() !=""){
            ageFrom = parseInt($("#ageFrom-year").val()*12) + parseInt($("#ageFrom-month").val()*1);
        }else{
            ageFrom = "";
        }
        if($("#ageTo-year").val() !="" || $("#ageTo-month").val() !=""){
            ageTo = parseInt($("#ageTo-year").val()*12) + parseInt($("#ageTo-month").val()*1);
        }else{
            ageTo = "";
        }
        var params = $bb.getPageParams();
        if( params && params.followQueueId ){
            var queueId = params.followQueueId;
        }else{
            var queueId = $("#queue").val();
        }
        
        var data = JSON.stringify({
            data: {
                "args": JSON.stringify(args),
                "flag": 2,
                "patientName": $.trim($("#patientName").val()),
                "phoneNum": $.trim($("#phoneNum").val()),
                "queueId": queueId,
                "hisNo": $.trim($("#hisNo").val()),
                "ageFrom": ageFrom,
                "ageTo": ageTo,
            }
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        return data.rows;
    }
    /**
     * columns
     */
    var columnsHisNo = function(e){
        return e.hisNo1 ? e.hisNo1 : e.hisNo2 ? e.hisNo2 : e.hisNo3 ? e.hisNo3 : "-";
    }
    var columnsGender = function(e){
        return e.gender == 'F' ? "女" : "男";
    }
    var columnsWeight = function(e){
        return e.weight ? e.weight : "-";
    }
    var columnsData = function(){
        var columnsArr = [
            {
                "data":columnsHisNo,
                "className":"hisNo",
            },
            {"data":'patientName'},
            {"data":'phoneNum'},
            {"data":'age'},
            {
                "data": 'birthday',
                "className":"birthday",  
            },
            {"data":columnsGender},
            {"data":'pregnancyWeek'},
            {
                "data":columnsWeight,
                "className":"weight",
            },
            {"data":'followQueueName'},
            {"data":'createTime'},
            {
                "data": null,
                "className":"operate",  
            }
        ];
        return columnsArr;
    }
    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        $('td.operate', row).html('<div class="btn-look">查看</div>');
        $('td.operate', row).find(".btn-look").click(function () {
            var imgSrc = 'page/followup/imgs/comm/' + (data.gender == 'F'? 'girl':'boy') + '.png';
            var params = {
                patientSchemeId: data.patientSchemeId,
                followQueueId: data.followQueueId,
                patientName: data.patientName,
                imgSrc: imgSrc,
                queuename: data.followQueueName,
                patientId: data.patientId,
                unchecked: data.unchecked
            };
            $bb.openPage("outPatient",params);
        });
        return row;
    };
    /**
     * 搜索search
     */
    var onSearch = function(){
        $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
        var args = $datatableUi.table.api().ajax.params();   //获取dt请求参数
    }
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        $("#allPatients").prev("div.row").remove();
        $("#patientName, #hisNo, #phoneNum").bind("input propertychange",onSearch); 
        $("#queue").bind("change",onSearch); 
        $("#serchBtn").click(onSearch);
        $("#resetBtn").click(function(){
            $("#patientName,#phoneNum,#queue,#hisNo,#ageFrom-year,#ageFrom-month,#ageTo-year,#ageTo-month").val("");
            onSearch();
        });
    }
    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "followup/service/patient/patientListForDatatableQuery?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete,
        };
        return options;
    }

	return servers;
});